/* 
 * Cashbox is the legal property of its developers, whose names are listed in the copyright file included
 * with this source distribution.
 * 
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
 * General Public License as published by the Free Software Foundation; either version 2 of the License,
 * or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
 * Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program; if not,
 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#import "CBImportPanelController.h"
#import "CBImportViewController.h"
#import "CBImporter.h"

#define VIEW_Y_LOCATION		40

@interface CBImportPanelController (Private)

- (id)initWithDelegate:(id)del importer:(CBImporter *)importer;

- (void)updateButtonTitles;

- (void)complete:(id)sender;
- (void)cancel:(id)sender;

- (void)resizeForNewView;
- (void)willShowProgressView:(NSNotification *)notification;
- (void)willShowStandardView:(NSNotification *)notification;
- (void)didChangeScreen:(NSNotification *)notification;

@end

@implementation CBImportPanelController

+ (NSString *)nibName {
	return @"ImportPanel";
}

+ (void)runWithDelegate:(id)delegate importer:(CBImporter *)importer {
	CBImportPanelController *controller = [[self alloc] initWithDelegate:delegate importer:importer];
	controller->releaseWhenComplete = TRUE;
	[[controller window] makeKeyAndOrderFront:nil];
}

+ (void)beginSheetWithDelegate:(id)delegate importer:(CBImporter *)importer modalForWindow:(NSWindow *)window {
	CBImportPanelController *controller = [[self alloc] initWithDelegate:delegate importer:importer];
	controller->releaseWhenComplete = TRUE;
	[NSApp beginSheet:[controller window]
	   modalForWindow:window
		modalDelegate:nil
	   didEndSelector:nil
		  contextInfo:nil];
}


- (id)initWithDelegate:(id)del importer:(CBImporter *)importer {
	if (self = [super init]) {
		delegate = del;
		viewController = [[CBImportViewController alloc] init];
		[viewController setDelegate:self];
		[[NSNotificationCenter defaultCenter] addObserver:self
												 selector:@selector(willShowProgressView:)
													 name:CBImportViewControllerWillShowProgressViewNotification
												   object:viewController];
		[[NSNotificationCenter defaultCenter] addObserver:self
												 selector:@selector(willShowStandardView:)
													 name:CBImportViewControllerWillShowStandardViewNotification
												   object:viewController];
		[[NSNotificationCenter defaultCenter] addObserver:self
												 selector:@selector(didChangeScreen:)
													 name:CBImportViewControllerDidChangeScreenNotification
												   object:viewController];

		// make sure the viewController exists before setting the importer
		[self setImporter:importer];
	}
	return self;
}

- (void)dealloc {
	[[NSNotificationCenter defaultCenter] removeObserver:self];
	[viewController release];
	[super dealloc];
}

- (BOOL)windowShouldClose:(id)sender {
	if (releaseWhenComplete) { [self autorelease]; }
	return YES;
}

- (void)windowDidLoad {
		
	// set button titles
	[self updateButtonTitles];
	// center
	[[self window] center];
	
	// set the frame for the viewController's view
	NSView *view = [viewController view];
	if (view) {
		NSRect frame = [view frame];
		frame.origin.x = 0;
		frame.origin.y = 0;
		frame.size.width = [[[self window] contentView] frame].size.width;
		frame.size.height = [[[self window] contentView] frame].size.height;
		[view setFrame:frame];
		[[[self window] contentView] addSubview:view];
	}

	[defaultButton setHidden:YES];
	[otherButton setHidden:YES];
}

- (CBImporter *)importer {
	return [viewController importer];
}
- (void)setImporter:(CBImporter *)anImporter {
	[self willChangeValueForKey:@"importer"];
	[viewController setImporter:anImporter];
	[self didChangeValueForKey:@"importer"];
}

#pragma mark watching changes to the import view
// ----------------------------------------------------------------------------------------------------
// watching changes to the import view
// ----------------------------------------------------------------------------------------------------

- (void)willShowProgressView:(NSNotification *)notification {
	[self resizeForNewView];
}

- (void)willShowStandardView:(NSNotification *)notification {
	[self resizeForNewView];
}

- (void)didChangeScreen:(NSNotification *)notification {
	[self updateButtonTitles];
}

- (void)resizeForNewView {
	if (!NSEqualSizes([viewController preferredContentSize], NSZeroSize)) {
		
		NSRect windowRect = [[self window] frame];
		NSRect windowResizeRect = windowRect;
		NSSize preferredContentSize = [viewController preferredContentSize];
		windowResizeRect.size = [NSWindow frameRectForContentRect:
								 NSMakeRect(0, 0, preferredContentSize.width,
											preferredContentSize.height)
														styleMask:[[self window] styleMask]].size;

		NSRect contentRect = [[[self window] contentView] frame];
		NSView *contentView = [viewController view];
		
		if ([viewController canRespond]) {
			[defaultButton setHidden:NO];
			[otherButton setHidden:NO];
			if ([contentView frame].origin.y != VIEW_Y_LOCATION) {
				[contentView setFrame:
				 NSMakeRect(contentRect.origin.x, VIEW_Y_LOCATION,
							contentRect.size.width, contentRect.size.height - VIEW_Y_LOCATION)];
			}
			windowResizeRect.size.height += VIEW_Y_LOCATION;
		} else {
			if ([contentView frame].origin.y == VIEW_Y_LOCATION) {
				[contentView setFrame:
				 NSMakeRect(contentRect.origin.x, 0, contentRect.size.width,
							contentRect.size.height)];
			}			
			[defaultButton setHidden:YES];
			[otherButton setHidden:YES];
		}
		windowResizeRect.origin.x += round((float)(windowRect.size.width - windowResizeRect.size.width) / 2.0);
		windowResizeRect.origin.y += round(windowRect.size.height - windowResizeRect.size.height);
		
		[[self window] setFrame:windowResizeRect
						display:YES
						animate:YES];
	}
}

#pragma mark import view controller delegate
// ----------------------------------------------------------------------------------------------------
// import view controller delegate
// ----------------------------------------------------------------------------------------------------

- (void)importViewControllerHasCompletedAllScreens:(CBImportViewController *)controller {
	[self complete:nil];
}

- (void)importViewControllerAcceptedImport:(CBImportViewController *)controller {
	if (delegate && [delegate respondsToSelector:@selector(importPanelDidAcceptImport:)]) {
		[delegate importPanelDidAcceptImport:self];
	} else {
		[[self importer] importData];
	}
}

- (void)importViewControllerDidCancel:(CBImportViewController *)controller {
	[self cancel:nil];
}


#pragma mark interface methods
// ----------------------------------------------------------------------------------------------------
// interface methods
// ----------------------------------------------------------------------------------------------------

- (IBAction)clickDefault:(id)sender {
	[viewController showNextScreen:nil];
	[self updateButtonTitles];
}

- (IBAction)clickOther:(id)sender {
	[viewController showPreviousScreen:nil];
	[self updateButtonTitles];
}

- (void)cancel:(id)sender {
	if ([[self importer] importComplete]) {
#warning revert the import
	}
	
	[self closeWindow:nil];
	if (delegate && [delegate respondsToSelector:@selector(importPanelDidCancel:)]) {
		[delegate importPanelDidCancel:self];
	}		
}

- (void)complete:(id)sender {
	[self closeWindow:nil];
	if (delegate && [delegate respondsToSelector:@selector(importPanelDidComplete:)]) {
		[delegate importPanelDidComplete:self];
	}	
}

- (void)updateButtonTitles {
	if ([viewController continueButton] == CBImportViewControllerImportButton) {
		[defaultButton setTitle:FSLocalizedString(@"Import", nil)];
	} else if ([viewController continueButton] == CBImportViewControllerCompleteButton) {
		[defaultButton setTitle:FSLocalizedString(@"Complete", nil)];
	} else if ([viewController continueButton] == CBImportViewControllerContinueButton) {
		[defaultButton setTitle:FSLocalizedString(@"Continue", nil)];
	}
	
	if ([viewController goBackButton] == CBImportViewControllerCancelButton) {
		[otherButton setTitle:FSLocalizedString(@"Cancel", nil)];
	} else if ([viewController goBackButton] == CBImportViewControllerGoBackButton) {
		[otherButton setTitle:FSLocalizedString(@"Back", nil)];
	}
}

@end
